package com.newlink.building.userinfo.activity

import android.content.Intent
import android.view.View
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.model.bean.AuthKeyTypeCard
import com.newlink.building.common_base.model.bean.AuthResponseBody
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.SimpleDividerItemDecoration
import com.newlink.building.common_base.utils.ToastUtils
import com.newlink.building.common_base.widget.CustomDialog
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.adapter.Module_UserInfo_CardDataAdapter
import com.newlink.building.userinfo.databinding.ActivityAuthCardAddBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_AuthCardAddPresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AuthCardAddContract
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker


/**
 * @Author: Jake
 * @Date: 2023-12-14
 * @Description:
 */
class Module_UserInfo_AuthCardAddActivityNL
    : NL_BaseMvpActivity<Module_UserInfo_AuthCardAddContract.View, Module_UserInfo_AuthCardAddContract.Presenter>(),
    Module_UserInfo_AuthCardAddContract.View {

    companion object {
        const val EXTRA_MODE = "MODE"
        const val EXTRA_DATA = "DATA"
        const val MODE_ADD = 0
        const val MODE_EDIT = 1
    }

    private var mCardList: MutableList<AuthResponseBody> = mutableListOf()


    private lateinit var mAdapter: Module_UserInfo_CardDataAdapter

    override fun createPresenter(): Module_UserInfo_AuthCardAddContract.Presenter = Module_UserInfo_AuthCardAddPresenter()

//    override fun attachLayoutRes(): Int = R.layout.activity_auth_card_add

    private lateinit var mBinding: ActivityAuthCardAddBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityAuthCardAddBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView() {
        super.initView()
        // 门禁卡添加页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("AuthCardAddActivity", "initView")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("auth_card")

        mBinding.cardAddTitleBar.tvTitleBarCenter.text = getString(R.string.auth_card_title)
        mBinding.cardAddTitleBar.ivTitleBarRight.apply {
            visibility = View.VISIBLE
            setOnClickListener {
                startActivity(Intent(this@Module_UserInfo_AuthCardAddActivityNL, AuthCardActivityNL::class.java).apply {
                    putExtra("MODE", MODE_ADD)
                })
            }
        }

    }

    private fun removeCardData(item: AuthResponseBody) {
        // 删除门禁卡时的安全混淆
        Module_Obfuscation_ObfuscationHelper.beforeDatabaseOperation("CARD_DELETE", item.toString())
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(4, item.hashCode())

        CustomDialog(this).setMessage(getString(R.string.confirm_delete_card_info))
            .setPositiveButton(object : CustomDialog.OnClickListener {
                override fun onClick(dialog: CustomDialog) {
                    mPresenter?.removeCard(accessToken, "$userId", item)
                    dialog.dismiss()
                }
            }).setNegativeButton(object : CustomDialog.OnClickListener {
                override fun onClick(dialog: CustomDialog) {
                    dialog.dismiss()
                }
            }).show()
    }

    override fun initData() {


        mAdapter = Module_UserInfo_CardDataAdapter(mCardList).apply {

            setOnDeleteClickListener(object : Module_UserInfo_CardDataAdapter.OnDeleteClickListener {
                override fun onDeleteClick(item: AuthResponseBody) {
                    NLog.e("CARD delete clicked")
                    removeCardData(item)
                }
            })

            setOnFvlItemClickListener(object : Module_UserInfo_CardDataAdapter.OnItemClickListener {
                override fun onClick(item: AuthResponseBody) {
                    NLog.e("item click $item")
                    editCardData(item)
                }

            })

        }

        mBinding.authCardList.apply {
            adapter = mAdapter
            addItemDecoration(
                SimpleDividerItemDecoration(
                    this@Module_UserInfo_AuthCardAddActivityNL, getDrawable(R.drawable.transparent_selector),
                    resources.getDimension(com.newlink.building.common_base.R.dimen.dp_10).toInt()
                )
            )
        }
    }

    override fun onResume() {
        super.onResume()
        loadCardList()
    }

    private fun loadCardList() {
        NLog.e("[jake]...load card list...")
        showLoading()
        mPresenter?.getCardList(accessToken, "$userId")
    }

    private fun editCardData(item: AuthResponseBody) {
        startActivity(Intent(this@Module_UserInfo_AuthCardAddActivityNL, AuthCardActivityNL::class.java).apply {
            putExtra(EXTRA_MODE, MODE_EDIT)
            putExtra(EXTRA_DATA, item)
        })
    }


    override fun getCardListSuccess(total: Int, list: MutableList<AuthResponseBody>?) {
        hideLoading()

        if (total == 0 || list == null) {
            mCardList.clear()
        } else {
            if (list.size != 0) {
                if (list[0].keyType != AuthKeyTypeCard) {
                    return
                }
            }
            mCardList = list

        }
        NLog.e("[jake]...get card list... $mCardList")
        mAdapter.setList(mCardList)
//        mAdapter.notifyDataSetChanged()
        updateEmptyView()
    }

    override fun getCardListFailed() {
        hideLoading()
        showToast(getString(R.string.get_card_list_failed))
        updateEmptyView()
    }

    override fun onAddCardDataSuccess() {
        showToast(getString(R.string.add_card_success))
        loadCardList()
    }

    override fun onDeleteCardDataSuccess() {
        ToastUtils.showTextToast(
            this@Module_UserInfo_AuthCardAddActivityNL,
            getString(R.string.delete_result_success)
        )
        mPresenter?.getCardList(accessToken, "$userId")
//        loadCardList()
    }

    override fun onAddCardDataFailed() {
        showToast(getString(R.string.add_card_failed))
    }

    override fun onDeleteCardDataFailed() {
        showToast(getString(R.string.delete_result_failed))
    }

    private fun updateEmptyView() {

        findViewById<View>(R.id.empty_content).apply {
            visibility = if (mCardList.size == 0) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }

    }

}
